﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElCharro01.Models;

namespace ElCharro01.Areas.User.Controllers
{ 
    public class LocationController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /User/Location/

        public ViewResult Index()
        {
            int userId = (int)  Session["UserId"];
            Customer Customer = (Customer) db.UserSet.Single(u => u.ID == userId);
            return View(Customer.Location.ToList());
        }

        
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /User/Location/Create

        [HttpPost]
        public ActionResult Create(Location location)
        {
            if (ModelState.IsValid)
            {
                int userId = (int)Session["UserId"];
                location.Customer = (Customer)db.UserSet.Single(u => u.ID == userId);
                db.LocationSet.AddObject(location);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(location);
        }
        
        //
        // GET: /User/Location/Edit/5
 
        public ActionResult Edit(int id)
        {
            Location location = db.LocationSet.Single(l => l.Id == id);
            return View(location);
        }

        //
        // POST: /User/Location/Edit/5

        [HttpPost]
        public ActionResult Edit(Location location)
        {
            if (ModelState.IsValid)
            {
                db.LocationSet.Attach(location);
                db.ObjectStateManager.ChangeObjectState(location, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(location);
        }

        //
        // GET: /User/Location/Delete/5
 
        public ActionResult Delete(int id)
        {
            Location location = db.LocationSet.Single(l => l.Id == id);
            return View(location);
        }

        //
        // POST: /User/Location/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Location location = db.LocationSet.Single(l => l.Id == id);
            db.LocationSet.DeleteObject(location);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}